package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.*;
import com.project.mapper.*;
import com.project.service.ISellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class SellServiceImpl implements ISellService {
    @Autowired
    private ISellMapper sellMapper;
    @Autowired
    private ISellCarMapper sellCarMapper;
    @Autowired
    private ISellInfoMapper sellInfoMapper;
    @Autowired
    private IProductMapper productMapper;
    @Autowired
    private IClientMapper clientMapper;

    /**
     * 添加销售订单
     * 1.计算该用户销售车的订单总额，然后，添加至销售对象
     * 2.完成销售对象添加
     * 3.查询登陆对象用户对应的销售车信息
     * 4.根据销售车信息添加销售明细表，同时减少商品的库存数量
     * 5.采购订单添加成功后清空销售车
     *
     * @param userId   用户编号
     * @param sellBean 销售对象
     */
    @Override
    public void add(Integer userId, SellBean sellBean) {
//        QueryWrapper<SellBean> qw = new QueryWrapper<>();
//        qw.eq("s_name", sellBean.getUser());
//        qw.eq("s_date", sellBean.getDate());
//        qw.eq("s_address", sellBean.getAddress());
//        qw.eq("s_allMoney", sellBean.getAllMoney());
//        qw.eq("fk_clientId", sellBean.getUser());

        sellBean.setAllMoney(sellCarMapper.getSellMoney(userId));
        sellMapper.insert(sellBean);

        QueryWrapper<SellCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId", userId);
        List<SellCarBean> sellCarList = sellCarMapper.selectList(qw);

        sellBean.setAllMoney(0);
        sellCarList.forEach(e -> {
            ProductBean product = productMapper.selectById(e.getProductId());
            e.setProduct(product);
            sellBean.setAllMoney(e.getNum() * product.getSellMoney() + sellBean.getAllMoney());
        });
        sellMapper.insert(sellBean);

        sellCarList.forEach(e -> {
            SellInfoBean sellInfo = new SellInfoBean();
            sellInfo.setSellId(sellBean.getId());
            sellInfo.setNum(e.getNum());
            sellInfo.setProductId(e.getProductId());
            sellInfo.setSellMoney(e.getProduct().getSellMoney());

            sellInfoMapper.insert(sellInfo);
            ProductBean product = productMapper.selectById(e.getProductId());
            product.setNum(product.getNum() - e.getNum());
            productMapper.updateById(product);
        });
//        infoMapper.addSellInfo(sell.getId(),sellCarList);
//        productMapper.updateSellInfo(sellCarList);

        sellCarMapper.delete(qw);
    }

    /**
     * 动态条件查询
     *
     * @param pageNo     页码
     * @param clientName 客户名称
     * @param startDate  起始日期
     * @param endDate    结束日期
     * @return 分页对象
     */
    @Override
    public IPage<SellBean> findByItem(Integer pageNo, String clientName, LocalDate startDate, LocalDate endDate) {
        return sellMapper.findByItem(new Page<>(pageNo, 3), clientName, startDate, endDate);
    }

    /**
     * 按编号查询信息，同时查询客户名称，并且查询销售的明细，在销售明细中，需要查询销售商品的信息
     *
     * @param id 销售编号
     * @return 销售对象
     */
    @Override
    public SellBean findById(Integer id) {
//        QueryWrapper<SellBean> qw = new QueryWrapper<>();
//        qw.eq("pk_sellId", id);
//        SellBean sell = sellMapper.selectOne(qw);
//        sell.setClient(clientMapper.findCli(id));
//        sell.setSellInfo(sellInfoMapper.findInfo(id));
//        return sell;

        SellBean sellBean = sellMapper.selectById(id);
        sellBean.setSellInfo(sellInfoMapper.findList(id));
        sellBean.setClient(clientMapper.findBySaleId(id));
        return sellBean;
    }
}
